/*   1:    */ package org.intellij.plugins.relaxNG.compact.lexer;
/*   2:    */ 
/*   3:    */

import com.intellij.lexer.LexerBase;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.psi.TokenType;
import com.intellij.psi.tree.IElementType;
import com.intellij.util.text.CharArrayCharSequence;
import com.intellij.util.text.CharArrayUtil;
import gnu.trove.TIntIntHashMap;
import org.intellij.plugins.relaxNG.compact.RncTokenTypes;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.kohsuke.rngom.parse.compact.CharStream;
import org.kohsuke.rngom.parse.compact.CompactSyntaxTokenManager;
import org.kohsuke.rngom.parse.compact.Token;
import org.kohsuke.rngom.parse.compact.TokenMgrError;

import java.io.CharArrayReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.lang.reflect.Field;
import java.net.URL;
import java.security.CodeSource;
import java.util.Arrays;
import java.util.LinkedList;

/*   4:    */
/*   5:    */
/*   6:    */
/*   7:    */
/*   8:    */
/*   9:    */
/*  10:    */
/*  11:    */
/*  12:    */
/*  13:    */
/*  14:    */
/*  15:    */
/*  16:    */
/*  17:    */
/*  18:    */
/*  19:    */
/*  20:    */
/*  21:    */
/*  22:    */
/*  23:    */
/*  24:    */
/*  25:    */
/*  26:    */
/*  27:    */
/*  28:    */
/*  29:    */
/*  30:    */
/*  31:    */ 
/*  32:    */ public class CompactSyntaxLexerAdapter
/*  33:    */   extends LexerBase
/*  34:    */ {
/*  35:    */   private static final Logger a;
/*  36:    */   private static final Field b;
/*  37:    */   
/*  38:    */   static
/*  39:    */   {
/*  40: 48 */     a = Logger.getInstance(CompactSyntaxLexerAdapter.class.getName());
/*  41:    */     try
/*  42:    */     {
/*  43: 54 */       b = CompactSyntaxTokenManager.class.getDeclaredField("curLexState");
/*  44: 55 */       b.setAccessible(true);
/*  45:    */     }
/*  46:    */     catch (NoSuchFieldException localNoSuchFieldException)
/*  47:    */     {
/*  48: 57 */       throw new Error(localNoSuchFieldException);
/*  49:    */     }
/*  50:    */   }
/*  51:    */   
/*  52: 61 */   private static final Token c = new Token();
/*  53:    */   private CompactSyntaxTokenManager d;
/*  54:    */   private final LinkedList<Token> e;
/*  55:    */   private Token f;
/*  56:    */   private int g;
/*  57:    */   private int h;
/*  58:    */   private IElementType i;
/*  59:    */   private CharSequence j;
/*  60:    */   private int k;
/*  61:    */   private TIntIntHashMap l;
/*  62:    */   
/*  63:    */   public CompactSyntaxLexerAdapter()
/*  64:    */   {
/*  65: 64 */     this.e = new LinkedList();
/*  66:    */   }
/*  67:    */   
/*  68:    */   public void advance()
/*  69:    */   {
/*  70:    */     try
/*  71:    */     {
/*  72: 76 */       this.f = a();
/*  73: 77 */       this.g = this.h;
/*  74: 79 */       if (this.f != null)
/*  75:    */       {
/*  76: 81 */         this.h = (this.g + this.f.image.length());
/*  77: 82 */         for (int m = this.g; m < this.h; m++) {
/*  78: 83 */           this.h += this.l.get(m);
/*  79:    */         }
/*  80: 86 */         if (this.f.kind == 0)
/*  81:    */         {
/*  82: 87 */           assert (this.g == this.k) : ("actual: " + this.g + ", expected: " + this.k);
/*  83: 88 */           this.f = null;
/*  84:    */         }
/*  85:    */       }
/*  86:    */     }
/*  87:    */     catch (TokenMgrError localTokenMgrError)
/*  88:    */     {
/*  89: 96 */       a.error(localTokenMgrError);
/*  90: 97 */       this.f = null;
/*  91:    */     }
/*  92:100 */     if (this.f == null)
/*  93:    */     {
/*  94:101 */       this.i = null;
/*  95:    */     }
/*  96:    */     else
/*  97:    */     {
/*  98:103 */       this.i = RncTokenTypes.get(this.f.kind);
/*  99:106 */       if (RncTokenTypes.WHITESPACE.contains(this.i)) {
/* 100:107 */         this.i = TokenType.WHITE_SPACE;
/* 101:    */       }
/* 102:    */     }
/* 103:    */   }
/* 104:    */   
/* 105:    */   private Token a()
/* 106:    */   {
/* 107:113 */     if (this.e.size() > 0) {
/* 108:114 */       return (Token)this.e.removeFirst();
/* 109:    */     }
/* 110:117 */     Token localToken1 = this.d.getNextToken();
/* 111:118 */     if (localToken1.specialToken != null)
/* 112:    */     {
/* 113:119 */       this.e.addFirst(localToken1);
/* 114:120 */       for (Token localToken2 = localToken1.specialToken; localToken2 != null; localToken2 = localToken2.specialToken) {
/* 115:121 */         this.e.addFirst(localToken2);
/* 116:    */       }
/* 117:123 */       return (Token)this.e.removeFirst();
/* 118:    */     }
/* 119:125 */     return localToken1;
/* 120:    */   }
/* 121:    */   
/* 122:    */   @Deprecated
/* 123:    */   public char[] getBuffer()
/* 124:    */   {
/* 125:131 */     return CharArrayUtil.fromSequence(this.j);
/* 126:    */   }
/* 127:    */   
/* 128:    */   @NotNull
/* 129:    */   public CharSequence getBufferSequence()
/* 130:    */   {
/* 131:137 */     CharSequence tmp4_1 = this.j;
/* 132:137 */     if (tmp4_1 == null) {
/* 133:137 */       throw new IllegalStateException(String.format("@NotNull method %s.%s must not return null", new Object[] { "org/intellij/plugins/relaxNG/compact/lexer/CompactSyntaxLexerAdapter", "getBufferSequence" }));
/* 134:    */     }
/* 135:137 */     return tmp4_1;
/* 136:    */   }
/* 137:    */   
/* 138:    */   public int getBufferEnd()
/* 139:    */   {
/* 140:141 */     return this.k;
/* 141:    */   }
/* 142:    */   
/* 143:    */   public int getState()
/* 144:    */   {
/* 145:    */     try
/* 146:    */     {
/* 147:146 */       return ((Integer)b.get(this.d)).intValue();
/* 148:    */     }
/* 149:    */     catch (Exception localException) {}
/* 150:148 */     return -1;
/* 151:    */   }
/* 152:    */   
/* 153:    */   public int getTokenEnd()
/* 154:    */   {
/* 155:153 */     return this.h;
/* 156:    */   }
/* 157:    */   
/* 158:    */   public int getTokenStart()
/* 159:    */   {
/* 160:157 */     return this.f == null ? 0 : this.g;
/* 161:    */   }
/* 162:    */   
/* 163:    */   @Nullable
/* 164:    */   public IElementType getTokenType()
/* 165:    */   {
/* 166:162 */     if (this.f == null) {
/* 167:163 */       return null;
/* 168:    */     }
/* 169:165 */     return this.i;
/* 170:    */   }
/* 171:    */   
/* 172:    */   @Deprecated
/* 173:    */   public void start(char[] paramArrayOfChar, int paramInt1, int paramInt2, int paramInt3)
/* 174:    */   {
/* 175:171 */     this.j = new CharArrayCharSequence(paramArrayOfChar, paramInt1, paramInt2);
/* 176:    */     
/* 177:173 */     CharArrayReader localCharArrayReader = new CharArrayReader(paramArrayOfChar, paramInt1, paramInt2 - paramInt1);
/* 178:174 */     a(paramInt1, paramInt2, localCharArrayReader, paramInt3);
/* 179:    */   }
/* 180:    */   
/* 181:    */   public void start(@NotNull CharSequence paramCharSequence, int paramInt1, int paramInt2, int paramInt3)
/* 182:    */   {
/* 183:179 */     if (paramCharSequence == null) {
/* 184:179 */       throw new IllegalArgumentException(String.format("Argument %s for @NotNull parameter of %s.%s must not be null", new Object[] { "0", "org/intellij/plugins/relaxNG/compact/lexer/CompactSyntaxLexerAdapter", "start" }));
/* 185:    */     }
/* 186:179 */     this.j = paramCharSequence;
/* 187:    */     
/* 188:181 */     CharSequenceReader localCharSequenceReader = new CharSequenceReader(paramCharSequence, paramInt1, paramInt2);
/* 189:182 */     a(paramInt1, paramInt2, localCharSequenceReader, paramInt3);
/* 190:    */   }
/* 191:    */   
/* 192:    */   private void a(int paramInt1, int paramInt2, Reader paramReader, int paramInt3)
/* 193:    */   {
/* 194:187 */     this.k = paramInt2;
/* 195:188 */     this.l = new TIntIntHashMap();
/* 196:    */     
/* 197:190 */     this.d = a(paramInt3, new EscapePreprocessor(paramReader, paramInt1, this.l));
/* 198:    */     
/* 199:192 */     this.f = c;
/* 200:193 */     this.g = paramInt1;
/* 201:194 */     this.h = paramInt1;
/* 202:195 */     this.e.clear();
/* 203:196 */     advance();
/* 204:    */   }
/* 205:    */   
/* 206:    */   private static CompactSyntaxTokenManager a(int paramInt, EscapePreprocessor paramEscapePreprocessor)
/* 207:    */   {
/* 208:    */     try
/* 209:    */     {
/* 210:201 */       return new CompactSyntaxTokenManager(new SimpleCharStream(paramEscapePreprocessor, 1, 1), paramInt);
/* 211:    */     }
/* 212:    */     catch (NoSuchMethodError localNoSuchMethodError)
/* 213:    */     {
/* 214:203 */       CompactSyntaxTokenManager localCompactSyntaxTokenManager = CompactSyntaxTokenManager.class;
/* 215:204 */       a.error("Unsupported version of RNGOM in classpath. Please check your IDEA and JDK installation.", localNoSuchMethodError, new String[] { "Actual parameter types: " + Arrays.toString(localCompactSyntaxTokenManager.getConstructors()[0].getParameterTypes()), "Location of " + localCompactSyntaxTokenManager.getName() + ": " + a(localCompactSyntaxTokenManager), "Location of " + CharStream.class.getName() + ": " + a(CharStream.class) });
/* 216:    */       
/* 217:    */ 
/* 218:    */ 
/* 219:208 */       throw localNoSuchMethodError;
/* 220:    */     }
/* 221:    */   }
/* 222:    */   
/* 223:    */   private static String a(Class<?> paramClass)
/* 224:    */   {
/* 225:213 */     CodeSource localCodeSource = paramClass.getProtectionDomain().getCodeSource();
/* 226:214 */     if (localCodeSource != null)
/* 227:    */     {
/* 228:215 */       localObject = localCodeSource.getLocation();
/* 229:216 */       if (localObject != null) {
/* 230:217 */         return ((URL)localObject).toExternalForm();
/* 231:    */       }
/* 232:    */     }
/* 233:220 */     Object localObject = paramClass.getName().replace('.', '/') + ".class";
/* 234:221 */     ClassLoader localClassLoader = paramClass.getClassLoader();
/* 235:222 */     URL localURL = localClassLoader != null ? localClassLoader.getResource((String)localObject) : ClassLoader.getSystemResource((String)localObject);
/* 236:223 */     return localURL != null ? localURL.toExternalForm() : "<unknown>";
/* 237:    */   }
/* 238:    */   
/* 239:    */   public static void main(String[] paramArrayOfString)
/* 240:    */     throws IOException
/* 241:    */   {
/* 242:227 */     CompactSyntaxLexerAdapter localCompactSyntaxLexerAdapter = new CompactSyntaxLexerAdapter();
/* 243:228 */     localCompactSyntaxLexerAdapter.start(new CharArrayCharSequence(FileUtil.adaptiveLoadText(new FileReader(paramArrayOfString[0]))));
/* 244:229 */     while (localCompactSyntaxLexerAdapter.getTokenType() != null)
/* 245:    */     {
/* 246:230 */       System.out.println("token = " + localCompactSyntaxLexerAdapter.getTokenType());
/* 247:231 */       int m = localCompactSyntaxLexerAdapter.getTokenStart();
/* 248:232 */       System.out.println("start = " + m);
/* 249:233 */       int n = localCompactSyntaxLexerAdapter.getTokenEnd();
/* 250:234 */       System.out.println("end = " + n);
/* 251:235 */       CharSequence localCharSequence = localCompactSyntaxLexerAdapter.getBufferSequence().subSequence(m, n);
/* 252:236 */       System.out.println("t = " + localCharSequence);
/* 253:237 */       localCompactSyntaxLexerAdapter.advance();
/* 254:    */     }
/* 255:    */   }
/* 256:    */   
/* 257:    */   static class CharSequenceReader
/* 258:    */     extends Reader
/* 259:    */   {
/* 260:    */     private final CharSequence a;
/* 261:    */     private final int b;
/* 262:    */     private int c;
/* 263:    */     
/* 264:    */     public CharSequenceReader(CharSequence paramCharSequence, int paramInt1, int paramInt2)
/* 265:    */     {
/* 266:248 */       this.a = paramCharSequence;
/* 267:249 */       this.b = paramInt2;
/* 268:250 */       this.c = paramInt1;
/* 269:    */     }
/* 270:    */     
/* 271:    */     public void close() {}
/* 272:    */     
/* 273:    */     public int read(char[] paramArrayOfChar, int paramInt1, int paramInt2)
/* 274:    */     {
/* 275:257 */       if ((paramInt1 < 0) || (paramInt1 > paramArrayOfChar.length) || (paramInt2 < 0) || (paramInt1 + paramInt2 > paramArrayOfChar.length) || (paramInt1 + paramInt2 < 0)) {
/* 276:258 */         throw new IndexOutOfBoundsException();
/* 277:    */       }
/* 278:259 */       if (paramInt2 == 0) {
/* 279:260 */         return 0;
/* 280:    */       }
/* 281:263 */       if ((this.a instanceof CharArrayCharSequence))
/* 282:    */       {
/* 283:264 */         i = ((CharArrayCharSequence)this.a).readCharsTo(this.c, paramArrayOfChar, paramInt1, paramInt2);
/* 284:265 */         if (i < 0) {
/* 285:265 */           return -1;
/* 286:    */         }
/* 287:266 */         this.c += i;
/* 288:267 */         return i;
/* 289:    */       }
/* 290:270 */       int i = Math.min(paramInt2, this.b - this.c);
/* 291:271 */       if (i <= 0) {
/* 292:271 */         return -1;
/* 293:    */       }
/* 294:273 */       for (int j = 0; j < i; j++) {
/* 295:274 */         paramArrayOfChar[(j + paramInt1)] = this.a.charAt(j + this.c);
/* 296:    */       }
/* 297:277 */       this.c += i;
/* 298:278 */       return i;
/* 299:    */     }
/* 300:    */     
/* 301:    */     public int read()
/* 302:    */     {
/* 303:282 */       if (this.c >= this.b) {
/* 304:282 */         return -1;
/* 305:    */       }
/* 306:283 */       return this.a.charAt(this.c++);
/* 307:    */     }
/* 308:    */   }
/* 309:    */ }



/* Location:           E:\idea\

 * Qualified Name:     org.intellij.plugins.relaxNG.compact.lexer.CompactSyntaxLexerAdapter

 * JD-Core Version:    0.7.0.1

 */